//
//  TradView.m
//  RentTable
//
//  Created by mac on 2024/11/29.
//

#import "TradView.h"

@interface TradView()

@property (nonatomic, strong) UIButton *favorateButton;
@property (nonatomic, strong) UILabel *favorateLabel;

@property (nonatomic, strong) UIButton *shopCatButton;
@property (nonatomic, strong) UIButton *tenantButton;

@end

@implementation TradView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}


- (void)setRentModel:(RentModel *)rentModel {
    _rentModel = rentModel;
    self.favorateButton.selected = _rentModel.rentCollection;
    self.shopCatButton.selected = _rentModel.rentCart;
}

- (void)setUI {
    self.favorateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.favorateButton setBackgroundImage:[UIImage imageNamed:@"收藏-未选中"] forState:UIControlStateNormal];
    [self.favorateButton setBackgroundImage:[UIImage imageNamed:@"已收藏"] forState:UIControlStateSelected];
    [self.favorateButton addTarget:self action:@selector(favorateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.favorateButton];
    [self.favorateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.leading.mas_equalTo(28 * scaleW);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    self.favorateLabel = [[UILabel alloc] init];
    self.favorateLabel.text = @"收藏";
    self.favorateLabel.font = [UIFont pingFangSCRegularWithSize:10];
    self.favorateLabel.textColor = myRGBHex(0x333333);
    self.favorateLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.favorateLabel];
    [self.favorateLabel sizeToFit];
    [self.favorateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.favorateButton.mas_centerX);
        make.top.equalTo(self.favorateButton.mas_bottom).offset(4);
        make.height.mas_equalTo(12);
    }];
    
    self.shopCatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.shopCatButton addTarget:self action:@selector(shopCatButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.shopCatButton];
    [self.shopCatButton.titleLabel sizeToFit];
    self.shopCatButton.frame = CGRectMake(61 * scaleW, 6, 90, 44);
    self.shopCatButton.layer.cornerRadius = 22;
    self.shopCatButton.layer.masksToBounds = true;
    [self.shopCatButton setImage:[UIImage imageNamed:@"购物车空"] forState:UIControlStateNormal];
    [self.shopCatButton setImage:[UIImage imageNamed:@"购物车"] forState:UIControlStateSelected];
    CGFloat imageWidth = 20;
    CGFloat imageHeight = 20;
    self.shopCatButton.imageEdgeInsets = UIEdgeInsetsMake((self.shopCatButton.frame.size.height - imageHeight) / 2,
                                                          (self.shopCatButton.frame.size.width - imageWidth) / 2,
                                                          (self.shopCatButton.frame.size.height - imageHeight) / 2,
                                                          (self.shopCatButton.frame.size.width - imageWidth) / 2);
    [CommondHandle gradientTheView:self.shopCatButton bounds:self.shopCatButton.bounds colors:@[myRGBHex(0xE375FF),myRGBHex(0xFF0AA1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5) hasCornerRadius:22 shouldTakeBack:false];
    
    self.tenantButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.tenantButton setTitle:@"立即租赁" forState:UIControlStateNormal];
    [self.tenantButton setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    self.tenantButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.tenantButton addTarget:self action:@selector(tenantButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.tenantButton];
    [self.tenantButton.titleLabel sizeToFit];
    self.tenantButton.layer.cornerRadius = 22;
    self.tenantButton.layer.masksToBounds = true;
    self.tenantButton.backgroundColor = myRGBHex(0x121212);
    [self.tenantButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(6);
        make.trailing.mas_equalTo(-16);
        make.size.mas_equalTo(CGSizeMake(190 * scaleW, 44));
    }];
}

- (void)favorateButtonClicked:(UIButton *)sender {
    
    NSString *account = [LocalDataManager loadDataForKey:UserAccount];
    NSString *logStau = [LocalDataManager loadDataForKey:LogStatu];
    
    if (account && [logStau isEqualToString:@"LogIn"]) {
        if (self.favorateBlock) {
            self.favorateBlock();
        }
        sender.selected = !sender.selected;
        self.rentModel.rentCollection = sender.selected;
        [[RentManager sharedManager] updateRentModel:self.rentModel];
        [[RentManager sharedManager] saveRentDataToFile];
    }else {
        [QLToast makeToast:@"请先登陆" toView:self];
    }
    
    
    
}

- (void)shopCatButtonPressed:(UIButton *)sender {
    
    NSString *account = [LocalDataManager loadDataForKey:UserAccount];
    NSString *logStau = [LocalDataManager loadDataForKey:LogStatu];
    if (account && [logStau isEqualToString:@"LogIn"]) {
        sender.selected = !sender.selected;
        if (self.shopCatBlock) {
            self.shopCatBlock();
        }
        
        self.rentModel.rentCart = sender.selected;
        [[RentManager sharedManager] updateRentModel:self.rentModel];
        [[RentManager sharedManager] saveRentDataToFile];
    }else {
        [QLToast makeToast:@"请先登陆" toView:self];
    }
    
    
}

- (void)tenantButtonPressed:(UIButton *)sender {
    
    NSString *account = [LocalDataManager loadDataForKey:UserAccount];
    NSString *logStau = [LocalDataManager loadDataForKey:LogStatu];
    
    if (account && [logStau isEqualToString:@"LogIn"]) {
        if (self.tenantBlock) {
            self.tenantBlock();
        }
    }else {
        [QLToast makeToast:@"请先登陆" toView:self];
    }
    
    
}

@end
